package jdk14;

import java.util.List;

/**
 * JDK 17 新特性：出现 NullPointerException 异常时，Java将提供更详细的报错信息
 */
public class NullPointerExceptionDemo {
    /**
     * NullPointerException Demo 1
     */
    public static void nullPointerExceptionDemo1() {
        try {
            String str = null;
            str.length();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * NullPointerException Demo 2
     */
    public static void nullPointerExceptionDemo2() {
        try {
            List list = List.of(null);
            String str = (String) list.get(0);
            str.length();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        /**
         * java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
         * 	at jdk17.NullPointerExceptionMoreInfo.nullPointerExceptionDemo1(NullPointerExceptionMoreInfo.java:15)
         * 	at jdk17.NullPointerExceptionMoreInfo.main(NullPointerExceptionMoreInfo.java:35)
         */
//        nullPointerExceptionDemo1();

        /**
         * java.lang.NullPointerException: Cannot read the array length because "elements" is null
         * 	at java.base/java.util.List.of(List.java:1037)
         * 	at jdk17.NullPointerExceptionMoreInfo.nullPointerExceptionDemo2(NullPointerExceptionMoreInfo.java:26)
         * 	at jdk17.NullPointerExceptionMoreInfo.main(NullPointerExceptionMoreInfo.java:43)
         */
        nullPointerExceptionDemo2();
    }
}
